﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace jq9602_jq9603.View
{
    /// <summary>
    /// PieChartControl.xaml 的交互逻辑
    /// </summary>
    public partial class PieChartControl : UserControl
    {
        // 1. 定义OKCount依赖属性
        public static readonly DependencyProperty OKCountProperty =
            DependencyProperty.Register(
                nameof(OKCount),
                typeof(int),
                typeof(PieChartControl),
                new FrameworkPropertyMetadata(
                    0,
                    FrameworkPropertyMetadataOptions.AffectsRender,
                    OnCountChanged));

        // 2. 定义NGCount依赖属性
        public static readonly DependencyProperty NGCountProperty =
            DependencyProperty.Register(
                nameof(NGCount),
                typeof(int),
                typeof(PieChartControl),
                new FrameworkPropertyMetadata(
                    0,
                    FrameworkPropertyMetadataOptions.AffectsRender,
                    OnCountChanged));

        // 3. 定义YieldRate只读依赖属性
        private static readonly DependencyPropertyKey YieldRatePropertyKey =
            DependencyProperty.RegisterReadOnly(
                nameof(YieldRate),
                typeof(double),
                typeof(PieChartControl),
                new PropertyMetadata(0.0));

        public static readonly DependencyProperty YieldRateProperty = YieldRatePropertyKey.DependencyProperty;

        // 属性包装器
        public int OKCount
        {
            get => (int)GetValue(OKCountProperty);
            set => SetValue(OKCountProperty, value);
        }

        public int NGCount
        {
            get => (int)GetValue(NGCountProperty);
            set => SetValue(NGCountProperty, value);
        }

        public double YieldRate
        {
            get => (double)GetValue(YieldRateProperty);
            private set => SetValue(YieldRatePropertyKey, value);
        }

        public PieChartControl()
        {
            InitializeComponent();
            Loaded += (s, e) => UpdatePieChart();
        }

        // 属性变更回调
        private static void OnCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (PieChartControl)d;
            control.UpdateYieldRate();
            control.UpdatePieChart();
        }

        private void UpdateYieldRate()
        {
            YieldRate = (OKCount + NGCount) > 0 ?
                Math.Round(OKCount * 100.0 / (OKCount + NGCount), 2) : 0;
        }

        private void UpdatePieChart()
        {
            PieCanvas.Children.Clear();

            if (OKCount == 0 && NGCount == 0)
            {
                // 绘制灰色圆形表示无数据
                DrawPieSegment(360, Brushes.Gray, 0);
                return;
            }

            double total = OKCount + NGCount;
            double okAngle = OKCount / total * 360;
            double ngAngle = NGCount / total * 360;

            // 绘制OK扇形
            DrawPieSegment(okAngle, Brushes.Green, 0);
            // 绘制NG扇形
            DrawPieSegment(ngAngle, Brushes.Red, okAngle);
        }

        private void DrawPieSegment(double angle, Brush fill, double startAngle)
        {
            var path = new Path
            {
                Fill = fill,
                Data = CreateArcGeometry(100, 100, 80, startAngle, angle),
                ToolTip = angle > 0 ? $"{angle / 3.6:F1}%" : null
            };
            PieCanvas.Children.Add(path);
        }

        private Geometry CreateArcGeometry(double x, double y, double radius, double startAngle, double sweepAngle)
        {
            var pathGeometry = new PathGeometry();
            var arcSegment = new ArcSegment(
                new Point(
                    x + radius * Math.Cos((startAngle + sweepAngle) * Math.PI / 180),
                    y + radius * Math.Sin((startAngle + sweepAngle) * Math.PI / 180)),
                new Size(radius, radius),
                0, sweepAngle > 180, SweepDirection.Clockwise, true);

            var pathFigure = new PathFigure
            {
                StartPoint = new Point(x, y),
                Segments = {
                new LineSegment(
                    new Point(
                        x + radius * Math.Cos(startAngle * Math.PI / 180),
                        y + radius * Math.Sin(startAngle * Math.PI / 180)),
                    true),
                arcSegment
            },
                IsClosed = true
            };

            pathGeometry.Figures.Add(pathFigure);
            return pathGeometry;
        }
    }
}